package seckill

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/seckill"
	seckillReq "github.com/flipped-aurora/gin-vue-admin/server/model/seckill/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SeckillActivityApi struct{}

// CreateSeckillActivity 创建秒杀活动
// @Tags SeckillActivity
// @Summary 创建秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body seckill.SeckillActivity true "创建秒杀活动"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /seckillActivity/createSeckillActivity [post]
func (seckillActivityApi *SeckillActivityApi) CreateSeckillActivity(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var seckillActivity seckill.SeckillActivity
	err := c.ShouldBindJSON(&seckillActivity)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = seckillActivityService.CreateSeckillActivity(ctx, &seckillActivity)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteSeckillActivity 删除秒杀活动
// @Tags SeckillActivity
// @Summary 删除秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body seckill.SeckillActivity true "删除秒杀活动"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /seckillActivity/deleteSeckillActivity [delete]
func (seckillActivityApi *SeckillActivityApi) DeleteSeckillActivity(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	activityId := c.Query("activityId")
	err := seckillActivityService.DeleteSeckillActivity(ctx, activityId)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteSeckillActivityByIds 批量删除秒杀活动
// @Tags SeckillActivity
// @Summary 批量删除秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /seckillActivity/deleteSeckillActivityByIds [delete]
func (seckillActivityApi *SeckillActivityApi) DeleteSeckillActivityByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	activityIds := c.QueryArray("activityIds[]")
	err := seckillActivityService.DeleteSeckillActivityByIds(ctx, activityIds)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateSeckillActivity 更新秒杀活动
// @Tags SeckillActivity
// @Summary 更新秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body seckill.SeckillActivity true "更新秒杀活动"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /seckillActivity/updateSeckillActivity [put]
func (seckillActivityApi *SeckillActivityApi) UpdateSeckillActivity(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var seckillActivity seckill.SeckillActivity
	err := c.ShouldBindJSON(&seckillActivity)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = seckillActivityService.UpdateSeckillActivity(ctx, seckillActivity)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindSeckillActivity 用id查询秒杀活动
// @Tags SeckillActivity
// @Summary 用id查询秒杀活动
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param activityId query int true "用id查询秒杀活动"
// @Success 200 {object} response.Response{data=seckill.SeckillActivity,msg=string} "查询成功"
// @Router /seckillActivity/findSeckillActivity [get]
func (seckillActivityApi *SeckillActivityApi) FindSeckillActivity(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	activityId := c.Query("activityId")
	reseckillActivity, err := seckillActivityService.GetSeckillActivity(ctx, activityId)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reseckillActivity, c)
}

// GetSeckillActivityList 分页获取秒杀活动列表
// @Tags SeckillActivity
// @Summary 分页获取秒杀活动列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query seckillReq.SeckillActivitySearch true "分页获取秒杀活动列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /seckillActivity/getSeckillActivityList [get]
func (seckillActivityApi *SeckillActivityApi) GetSeckillActivityList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo seckillReq.SeckillActivitySearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := seckillActivityService.GetSeckillActivityInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetSeckillActivityPublic 不需要鉴权的秒杀活动接口
// @Tags SeckillActivity
// @Summary 不需要鉴权的秒杀活动接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /seckillActivity/getSeckillActivityPublic [get]
func (seckillActivityApi *SeckillActivityApi) GetSeckillActivityPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	activityId := c.Query("activityId")
	err := seckillActivityService.GetSeckillActivityPublic(ctx, activityId)
	if err != nil {
		global.GVA_LOG.Error("关闭失败!", zap.Error(err))
		response.FailWithMessage("关闭失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("关闭成功", c)
}
